package dt.member;


import java.io.IOException;
import java.io.Reader;
import java.util.ArrayList;
import java.util.List;

import com.ibatis.common.resources.Resources;
import com.ibatis.sqlmap.client.SqlMapClient;
import com.ibatis.sqlmap.client.SqlMapClientBuilder;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class MemberList extends ActionSupport{

	public static Reader reader;
	public static SqlMapClient sqlMapper;
	
	private List<memberVO> list = new ArrayList<memberVO>();

	private int searchN;
	private String search;


	private int currentPage = 1;
	private int totalCount;
	private int blockCount = 30;
	private int blockPage = 5;
	private String pagingHtml;
	private pagingAction page;


	public MemberList() throws IOException{
		reader = Resources.getResourceAsReader("sqlMapConfig.xml");
		sqlMapper = SqlMapClientBuilder.buildSqlMapClient(reader);
		reader.close();		
		
	}
	
	
	public String list() throws Exception{
		
		if(ActionContext.getContext().getSession().get("id")==null){
			return LOGIN;
		}else{
			if(!ActionContext.getContext().getSession().get("id").equals("admin")){
				return "adminError";
			}else{
				return execute();
			}
		}
	}
	
	public String execute() throws Exception {
		
		
	if(getSearch() != null){
			return search();
		}

		list = sqlMapper.queryForList("selectAll-m");
		
		totalCount = list.size();

		page = new pagingAction(currentPage, totalCount, blockCount, blockPage, "");
		pagingHtml = page.getPagingHtml().toString();
		
		int lastCount = totalCount;
		
		if(page.getEndCount() < totalCount)
			lastCount = page.getEndCount() + 1;
		
		list = list.subList(page.getStartCount(), lastCount);

		String memid = ActionContext.getContext().getSession().get("id").toString();
		
		
		
		return SUCCESS;
	}
	
	
	public String search() throws Exception {


		if(getSearchN()==0)
			list = sqlMapper.queryForList("selectSearch0-m", "%"+getSearch()+"%");
		else if(getSearchN()==1)
			list = sqlMapper.queryForList("selectSearch1-m", "%"+getSearch()+"%");
		else if(getSearchN()==2){
			List<memberVO> list2 = sqlMapper.queryForList("selectAll-m");
			for(int i=0 ; i<list2.size() ; i++){
				String mobile2 = list2.get(i).getMobile1()+list2.get(i).getMobile2()+list2.get(i).getMobile3();
				if(mobile2.equals(getSearch())){
					list.add(list2.get(i));
				}
			}
		}

		

		totalCount = list.size();
		page = new pagingAction(currentPage, totalCount, blockCount, blockPage, getSearch());
		pagingHtml = page.getPagingHtml().toString();
		
		int lastCount = totalCount;
		
		if(page.getEndCount() < totalCount)
			lastCount = page.getEndCount() + 1;
		
		list = list.subList(page.getStartCount(), lastCount);
		return SUCCESS;
	}



	public List<memberVO> getList() {
		return list;
	}

	public void setList(List<memberVO> list) {
		this.list = list;
	}

	public int getCurrentPage() {
		return currentPage;
	}

	public void setCurrentPage(int currentPage) {
		this.currentPage = currentPage;
	}

	public int getTotalCount() {
		return totalCount;
	}

	public void setTotalCount(int totalCount) {
		this.totalCount = totalCount;
	}

	public int getBlockCount() {
		return blockCount;
	}

	public void setBlockCount(int blockCount) {
		this.blockCount = blockCount;
	}

	public int getBlockPage() {
		return blockPage;
	}

	public void setBlockPage(int blockPage) {
		this.blockPage = blockPage;
	}

	public String getPagingHtml() {
		return pagingHtml;
	}

	public void setPagingHtml(String pagingHtml) {
		this.pagingHtml = pagingHtml;
	}


	public int getSearchN() {
		return searchN;
	}


	public void setSearchN(int searchN) {
		this.searchN = searchN;
	}


	public String getSearch() {
		return search;
	}


	public void setSearch(String search) {
		this.search = search;
	}
	
	



	

}
